package week08.horizontal;

import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author edward
 * @since 2021-08-14
 */
@Component
public class UserOrderRepository {
    private JdbcTemplate jdbcTemplate;

    @Transactional
    public void insert(int userId, String result) {
        jdbcTemplate.update("insert into t_user_order(user_id,result) "
                + "values(?,?)", userId, result);
    }

    @Transactional
    public void update(int userId, long orderId, String result) {
        jdbcTemplate.update("update t_user_order set result=? "
                + "where user_id=? and order_id=?", result, userId, orderId);
    }
    @Transactional
    public void deleteByOrderId(long orderId) {
        jdbcTemplate.update("delete from t_user_order "
                + "where order_id=?", orderId);
    }
    @Transactional
    public void deleteByUser(int userId) {
        jdbcTemplate.update("delete from t_user_order "
                + "where user_id=?", userId);
    }

    @Transactional
    @ShardingTransactionType(TransactionType.XA)
    public void xaInsert(int userId, String result) {
        jdbcTemplate.update("insert into t_user_order(user_id,result) "
                + "values(?,?)", userId, result);
    }
    @Transactional
    @ShardingTransactionType(TransactionType.XA)
    public void xaUpdate(int userId, long orderId, String result) {
        jdbcTemplate.update("update t_user_order set result=? "
                + "where user_id=? and order_id=?", result, userId, orderId);
    }
    @Transactional
    @ShardingTransactionType(TransactionType.XA)
    public void xaDeleteByUser(int userId) {
        jdbcTemplate.update("delete from t_user_order "
                + "where user_id=?", userId);
    }


    public List<UserOrder> selectAll() {
        return jdbcTemplate.query("select * from t_user_order", (resultSet, rowNum) -> {
            UserOrder order = new UserOrder();
            order.setUserId(resultSet.getInt("user_id"));
            order.setOrderId(resultSet.getLong("order_id"));
            order.setResult(resultSet.getString("result"));
            return order;
        });
    }
    public UserOrder selectById(int userId, long orderId) {
        return jdbcTemplate.queryForObject("select * from t_user_order where user_id=? and order_id=?", (resultSet, rowNum) -> {
            UserOrder order = new UserOrder();
            order.setUserId(resultSet.getInt("user_id"));
            order.setOrderId(resultSet.getLong("order_id"));
            order.setResult(resultSet.getString("result"));
            return order;
        }, userId, orderId);
    }

    public List<UserOrder> selectByUserId(int userId) {
        return jdbcTemplate.query("select * from t_user_order where user_id=?", (resultSet, rowNum) -> {
            UserOrder order = new UserOrder();
            order.setUserId(resultSet.getInt("user_id"));
            order.setOrderId(resultSet.getLong("order_id"));
            order.setResult(resultSet.getString("result"));
            return order;
        }, userId);
    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
